package com.goodfather.textbook.youdao.pad;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;

import androidx.multidex.MultiDex;

import com.goodfather.textbook.youdao.pad.util.C;
import com.lzy.okgo.OkGo;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: zhou
 * @CreateDate: 2021/11/27
 */
public class GoodFatherApplication extends Application {

    private static GoodFatherApplication application;
    // 管理多页面Bot的全部Activity,在退出意图收到的时候，finish掉所有的activity
    private static List<WeakReference<Activity>> activities = new ArrayList<>();

    @Override
    public void onCreate() {
        super.onCreate();
        C.setContext(getApplicationContext());
        init();
    }

    public void init() {
        application = this;
        //华为初始化
        OkGo.getInstance().init(this);
    }



    @Override
    public void onTerminate() {
        super.onTerminate();

    }

    /**
     * 根据Pid得到进程名
     */
    public static String getAppNameByPID(Context context, int pid) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (android.app.ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
            if (processInfo.pid == pid) {
                return processInfo.processName;
            }
        }
        return "";
    }


    //方法超过66535
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            MultiDex.install(base);
        }
    }

    public static void exitApp() {
        for (WeakReference<Activity> ref : activities) {
            Activity activity = ref.get();
            if (activity != null) {
                activity.finish();
            }
        }
    }

}
